﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using RegulatoryPlatform.DB;
using RegulatoryPlatform.Models;
using RegulatoryPlatform.Tools;

namespace RegulatoryPlatform.ScheduleTask
{
    /// <summary>
    /// 药品配送信息
    /// </summary>
    public class YL_YPPSXX : Base
    {
        public override async Task Execute(IJobExecutionContext context)
        {
            string param = DateTime.Now.ToString("yyyyMMdd") + "235959";
            var json = rDData.GetDataByMethod("Address_Get_YPPSXX", new YPPSXXRequest
            {
                PSJSRQ = param
            });

            var response = Serialize<YPPSXX>(json);
            if (response.status == 10000 && response.msg == "success")
            {
                try
                {


                    List<interface_adviceorder> regList = new List<interface_adviceorder>();
                    if (response.content.Count > 0)
                    {
                        string[] regIdList = response.content.Select(s => s.fzlsh).ToArray();

                        regList = hlwDB.interface_adviceorder.AsNoTracking().Where(s => regIdList.Contains(s.regid)).Select(s => new interface_adviceorder
                        {
                            regid = s.regid,
                            prescriptionNo = s.prescriptionNo
                        }).ToList();
                    }

                    //TODO:药品配送信息 已完成
                    foreach (var item in response.content)
                    {
                        var result = regList.FirstOrDefault(s => s.regid == item.fzlsh);
                        if (result != null)
                        {
                            TB_YL_YPPSXX entity = new TB_YL_YPPSXX();
                            entity.YLJGDM = YLJGDM;
                            entity.CYH = Guid.NewGuid().ToString();
                            entity.FZLSH = item.fzlsh;
                            entity.WSJGDM = WSJGDM;
                            entity.PSFS = item.psfs;
                            entity.PSKSSJ = DateTime.Parse(item.pskssj);
                            entity.PSJSSJ = DateTime.Parse(item.psjssj);
                            entity.PSZT = item.pszt;
                            entity.XGBZ = item.xgbz;

                            db.Set<TB_YL_YPPSXX>().Add(entity);
                        }
                        else
                        {
                            Logger.WriteLog("未能找到:{0} 的处方号", item.fzlsh);
                        }
                    }

                    await db.BulkSaveChangesAsync();
                    await Console.Out.WriteLineAsync($"药品配送信息 数据同步完成 在{DateTime.Now}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"药品配送信息 出错了:{ex.Message}");
                    JobExecutionException e2 = new JobExecutionException(ex);
                    // 这个工作将立即重新开始  
                    e2.RefireImmediately = true;
                    throw e2;
                }
            }
            else
            {
                await Console.Out.WriteLineAsync($"未能找到 {param} 对应的 药品配送信息");
            }
        }
    }

    /// <summary>
    /// 药品配送信息 入参
    /// </summary>
    public class YPPSXXRequest
    {
        /// <summary>
        /// 配送结束时间YYYYMMDD
        /// </summary>
        public string PSJSRQ { get; set; }
    }
}
